package org.evan.spring6.resources;

import org.springframework.core.io.ClassPathResource;

import java.io.InputStream;

//访问类路径下资源
public class ClassPathResourceDemo {

    public static void loadClassPathResources(String Path){
        try {
            //创建对象ClassPathResource
            ClassPathResource resource = new ClassPathResource(Path);

            System.out.println(resource.getFilename());
            System.out.println(resource.getDescription());
            //获取文件内容
            InputStream in = resource.getInputStream();
            byte[] bytes = new byte[1024];
            while (in.read(bytes) != -1){
                System.out.println(new String(bytes));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // 读取classpath下txt文件
        loadClassPathResources("evan.txt");
    }
}
